package com.nuotu.datawall.clouddata;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.blankj.utilcode.util.ThreadUtils;
import com.kongzue.dialog.v3.MessageDialog;
import com.nuotu.datawall.clouddata.base.BaseActivity;
import com.nuotu.datawall.clouddata.control.DateControl;
import com.nuotu.datawall.clouddata.layoutData.BasicDataLayout;
import com.nuotu.datawall.clouddata.layoutData.BorrowCountChar10tLayout;
import com.nuotu.datawall.clouddata.layoutData.BorrowCountChartLayout;
import com.nuotu.datawall.clouddata.layoutData.BorrowCountLayout;
import com.nuotu.datawall.clouddata.layoutData.DepartmentCountLayout;
import com.nuotu.datawall.clouddata.layoutData.KindCollectionPieLayout;
import com.nuotu.datawall.clouddata.layoutData.NewtLayout;
import com.nuotu.datawall.clouddata.layoutData.ReaderCountLayout;
import com.nuotu.datawall.clouddata.layoutData.ReaderCountPieLayout;
import com.nuotu.datawall.clouddata.layoutData.SpecialLayout;
import com.nuotu.datawall.clouddata.util.FinalStr;
import com.nuotu.datawall.clouddata.util.LogCook;
import com.nuotu.datawall.clouddata.util.TimeTaskScroll;
import com.nuotu.datawall.clouddata.util.Tool;
import com.nuotu.datawall.clouddata.util.XUtilsImage;
import com.nuotu.datawall.clouddata.view.ConfirmAlert;
import com.nuotu.datawall.clouddata.view.EchartView;

import org.xutils.common.task.AbsTask;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;


@ContentView(R.layout.activity_main)
public class MainActivity extends BaseActivity {

    @ViewInject(R.id.top_time)
    private TextView top_time;
    @ViewInject(R.id.gv_jcsj)
    private GridView gv_jcsj;

    @ViewInject(R.id.borrowCountChart7)
    private EchartView borrowCountChart7;

    @ViewInject(R.id.borrowCountChart10)
    private EchartView borrowCountChart10;

    @ViewInject(R.id.kindCollectionPie)
    private EchartView kindCollectionPie;
    @ViewInject(R.id.kindLine)
    private ImageView kindLine;
    @ViewInject(R.id.readerLine)
    private ImageView readerLine;
    @ViewInject(R.id.borrowLine)
    private ImageView borrowLine;
    @ViewInject(R.id.specialLine)
    private ImageView specialLine;
    @ViewInject(R.id.departmentBorrowColumn)
    private EchartView departmentBorrowColumn;
    @ViewInject(R.id.readerCountTop10)
    private ListView readerCountTop10;
    @ViewInject(R.id.view_flipper)
    private ViewFlipper view_flipper;
    @ViewInject(R.id.gv_borrowCount)
    private GridView gv_borrowCount;
    @ViewInject(R.id.main_config)
    private RelativeLayout main_config;

//    @ViewInject(R.id.gv_specialCount)
//    private GridView gv_specialCount;



    private  BasicDataLayout basicDataLayout;
    private  BorrowCountChartLayout borrowCountChartLayout;
    private  BorrowCountChar10tLayout borrowCountChar10tLayout;
    private  KindCollectionPieLayout kindCollectionPieLayout;
    private  ReaderCountPieLayout readerCountPieLayout;
    private  DepartmentCountLayout departmentCountLayout;
    private  ReaderCountLayout readerCountLayout;
    private  NewtLayout newtLayout;
    private BorrowCountLayout borrowCountLayout;
    private SpecialLayout specialLayout;

    private ThreadUtils.Task refreshTask;
    private ThreadUtils.Task changeTask;
    private ThreadUtils.Task gundongTask;
    private ThreadUtils.Task restartAppTask;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
//        String loginError=getIntent().getStringExtra(FinalStr.LOGIN_ERROR_KEY);
//        if("Y".equals(loginError)){
//            MessageDialog.show(this, "提示", "这是一条消息", "确定");
//        }
    }

    private void initData() {
        XUtilsImage.getInstance().init(mContext);
        //基础数据
        basicDataLayout=  BasicDataLayout.getInstance(mContext).init(gv_jcsj); //
        //借阅量热力分布图
        borrowCountChartLayout=BorrowCountChartLayout.getInstance(mContext).init(borrowCountChart7);
        //借阅量变化10天
        borrowCountChar10tLayout= BorrowCountChar10tLayout.getInstance(mContext).init(borrowCountChart10);
        //五大部类馆藏占比
        kindCollectionPieLayout = KindCollectionPieLayout.getInstance(mContext).init(kindCollectionPie);
        //读者借阅人数占比
        readerCountPieLayout = ReaderCountPieLayout.getInstance(mContext).init(kindCollectionPie);
        //部门借阅量top5 本月
        departmentCountLayout = DepartmentCountLayout.getInstance(mContext).init(departmentBorrowColumn);
        //读者借阅量 top10 (当月)
        readerCountLayout = ReaderCountLayout.getInstance(mContext).init(readerCountTop10);
        //消息滚动
        newtLayout=NewtLayout.getInstance(mContext).init(view_flipper);
        //借阅量top6
        borrowCountLayout=BorrowCountLayout.getInstance(mContext).init(gv_borrowCount);
        //专题阅读书单
//      specialLayout=SpecialLayout.getInstance(mContext).init(gv_specialCount);

        borrowCountChart10.setBackgroundColor(Color.argb(70,8,19,51)); // 设置背景色
        borrowCountChart7.setBackgroundColor(Color.argb(70,8,19,51)); // 设置背景色
        kindCollectionPie.setBackgroundColor(Color.argb(70,8,19,51)); // 设置背景色
        departmentBorrowColumn.setBackgroundColor(Color.argb(70,8,19,51)); // 设置背景色

        initListener();
    }
    private void initListener() {
        //开启时间控件
        DateControl.getInstance().init(top_time).toStart();
        refreshTask=new ThreadUtils.Task() {
            @Override
            public Object doInBackground() throws Throwable {
                basicDataLayout.refreshData();
                borrowCountChartLayout.refreshData();
                borrowCountChar10tLayout.refreshData();
                kindCollectionPieLayout.refreshData();
                readerCountPieLayout.refreshData();
                departmentCountLayout.refreshData();
                readerCountLayout.refreshData();
                newtLayout.refreshData();
                borrowCountLayout.refreshData();
                return null;
            }
            @Override
            public void onSuccess(Object result) {

            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onFail(Throwable t) {

            }
        };
        changeTask=new ThreadUtils.Task() {
            @Override
            public Object doInBackground() throws Throwable {
                x.task().post(new Runnable() {
                    @Override
                    public void run() {
                        if(kindLine.getVisibility()== View.INVISIBLE){
                            kindCollectionPieLayout.show();
                            kindLine.setVisibility(View.VISIBLE);
                            readerLine.setVisibility(View.INVISIBLE);
//                            borrowLine.setVisibility(View.VISIBLE);
//                            specialLine.setVisibility(View.INVISIBLE);
//                            gv_borrowCount.setVisibility(View.VISIBLE);
//                            gv_specialCount.setVisibility(View.GONE);
                        }else {
                            readerCountPieLayout.show();
                            kindLine.setVisibility(View.INVISIBLE);
                            readerLine.setVisibility(View.VISIBLE);
//                            borrowLine.setVisibility(View.INVISIBLE);
//                            specialLine.setVisibility(View.VISIBLE);
//                            gv_borrowCount.setVisibility(View.GONE);
//                            gv_specialCount.setVisibility(View.VISIBLE);
                        }
                    }
                });
                return null;
            }
            @Override
            public void onSuccess(Object result) {
            }
            @Override
            public void onCancel() {
            }
            @Override
            public void onFail(Throwable t) {

            }
        };
        gundongTask =new ThreadUtils.Task() {
            @Override
            public Object doInBackground() throws Throwable {
                x.task().post(new Runnable() {
                    @Override
                    public void run() {
                        readerCountTop10.smoothScrollBy(1, 0);
                    }
                });
                return null;
            }
            @Override
            public void onSuccess(Object result) {
            }
            @Override
            public void onCancel() {

            }
            @Override
            public void onFail(Throwable t) {

            }
        };
        restartAppTask =new ThreadUtils.Task() {
            @Override
            public Object doInBackground() throws Throwable {
                onDestroy();
                Tool.restartApplication(mContext);
                return null;
            }
            @Override
            public void onSuccess(Object result) {
            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onFail(Throwable t) {

            }
        };

//        TimeUnit.DAYS          //天
//        TimeUnit.HOURS         //小时
//        TimeUnit.MINUTES       //分钟
//        TimeUnit.SECONDS       //秒
//        TimeUnit.MILLISECONDS  //毫秒
        ThreadUtils.executeByCachedAtFixRate(gundongTask,1000,100, TimeUnit.MILLISECONDS); //在缓冲线程池按固定频率执行任务
        ThreadUtils.executeByCpuAtFixRate(changeTask,1000*3,1000*33,TimeUnit.MILLISECONDS);//在 CPU 线程池按固定频率执行任务
        ThreadUtils.executeByIoAtFixRate(refreshTask,0,60000*10,TimeUnit.MILLISECONDS);// 在 IO 线程池按固定频率执行任务
        ThreadUtils.executeByCpuAtFixRate(restartAppTask,6000*60*2,TimeUnit.MILLISECONDS);
        main_config.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ConfirmAlert.getInstance().init(MainActivity.this).setOk(new ConfirmAlert.ToDo() {
                    @Override
                    public void todo() {
                        startActivity(new Intent(mContext, SettingActivity.class));
                    }
                });
            }
        });
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        ThreadUtils.cancel(gundongTask);
        ThreadUtils.cancel(refreshTask);
        ThreadUtils.cancel(changeTask);
        ThreadUtils.cancel(restartAppTask);
        LogCook.e("onDestroy","崩溃了");
        DateControl.getInstance().toStop();
    }

}
